package com.niit.util;

import com.sun.mail.util.MailSSLSocketFactory;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.InputStream;
import java.util.Properties;
import java.util.Random;

public class EmailUtil {
    private static String qq;
    private static String au_code;
    private static String senderEmail;

    static {

        try{
            ClassLoader classLoader = EmailUtil.class.getClassLoader();
            InputStream is = classLoader.getResourceAsStream("email.properties");
            Properties properties = new Properties();
            properties.load(is);

            qq = properties.getProperty("qq");
            au_code = properties.getProperty("au_code");
            senderEmail = properties.getProperty("senderEmail");

        }catch(Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 利用指定QQ服务器发送邮件
     * @throws Exception 处理异常
     * @return 生成的验证码
     */
    public static String sendEmail(String receiverEmail) throws Exception{
        Properties props = new Properties();

        // 发送服务器需要身份验证
        props.setProperty("mail.smtp.auth", "true");
        // 设置邮件服务器主机名
        props.setProperty("mail.host", "smtp.qq.com");
        // 发送邮件协议名称
        props.setProperty("mail.transport.protocol", "smtp");

        /**SSL认证，注意腾讯邮箱是基于SSL加密的，所有需要开启才可以使用**/
        MailSSLSocketFactory sf = new MailSSLSocketFactory();
        sf.setTrustAllHosts(true);
        props.put("mail.smtp.ssl.enable", "true");
        props.put("mail.smtp.ssl.socketFactory", sf);

        //创建会话
        Session session = Session.getInstance(props);

       //  获取4位数字随机验证码
       Random random =new Random();
       String code="";
       for (int j = 0; j < 4; j++) {
           int i = random.nextInt(9);
           code = code + i;
       }

        Transport ts = session.getTransport();
        // 连接邮件服务器：邮箱类型，帐号，POP3/SMTP协议授权码 163使用：smtp.163.com
        ts.connect("smtp.qq.com", qq, au_code);
        // 创建邮件
        Message message = createSimpleMail(session,senderEmail,receiverEmail,code);
        // 发送邮件
        ts.sendMessage(message, message.getAllRecipients());
        ts.close();
        return code;
    }

    /**
     * @Method: createSimpleMail
     * @Description: 创建一封只包含文本的邮件
     */
     public static MimeMessage createSimpleMail(Session session, String senderEmail, String receiverEmail, String code) throws Exception {
         // 创建邮件对象
         MimeMessage message = new MimeMessage(session);
         // 指明邮件的发件人  发送方QQ@qq.com
         message.setFrom(new InternetAddress(senderEmail));
         // 指明邮件的收件人，发件人和收件人如果是一样的，那就是自己给自己发   接受方QQ@qq.com
         message.setRecipient(Message.RecipientType.TO, new InternetAddress(receiverEmail));
         // 邮件的标题
         message.setSubject("【大数据分析平台账号注册验证码】");
         // 邮件的文本内容
         message.setContent("欢迎您注册【大数据分析平台】,账号注册验证码为(一分钟有效): "+code+" ,请勿回复此邮箱", "text/html;charset=UTF-8");
         // 返回创建好的邮件对象
         return message;
    }
}
